﻿local json = require("cjson")

local  cmds = {}


function cmds:_query_(sql)

  
	local mysql = require "resty.mysql"
	local db, err = mysql:new()
	if not db then
	    ngx.log(ngx.ERR, "failed to instantiate mysql: ", err)
	    return
	end    
    db:set_timeout(1000) -- 1 sec
        
    local ok, err, errcode, sqlstate = db:connect{
        host = "127.0.0.1",
        port = 3306,
        database = "rpg_game",
        user = "mogo",
        password = "mogo4399",
        charset="utf8",
        max_packet_size = 1024 * 1024 }

    if not ok then
        ngx.log(ngx.ERR, "failed to connect: ", err, ": ", errcode, " ", sqlstate)
        return 12, "failed to connect mysql"
    end


    local res, err, errcode, sqlstate =  db:query("SET NAMES utf8")
    if not res then
        ngx.log(ngx.ERR, "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
        return 12, "bad result"
    end

    local res, err, errcode, sqlstate =  db:query(sql)
    if not res then
        ngx.log(ngx.ERR, "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
        return 12, "bad result"
    end
    
	-- put it into the connection pool of size 100,
	-- with 10 seconds max idle timeout
	local ok, err = db:set_keepalive(10000, 100)
	if not ok then
		ngx.log(ngx.ERR, "failed to set keepalive: ", err)
		return 12, "failed to set keepalive"
    end

    ngx.log(ngx.ERR, json.encode(res), sql)

    return 0, res, err, errcode, sqlstate

end




function cmds:add_gold(args)
	local pid = tonumber(args.pid)
	local num = tonumber(args.num)
	if (not pid) or (not num) or num == 0 then
		return 11, "args error"
	end

	local cmd_t = {}
	cmd_t.cmd = "set_value"
	cmd_t.pid = pid
	cmd_t.props = {}
	table.insert(cmd_t.props,{1 ,"fangka", num}) --0设置,1增加

	local json_str = json.encode(cmd_t)

	local sql = string.format("insert into gm_cmd_moudle (info) VALUES ('%s')",json_str)

	local ret, res = self:_query_(sql)  

	if ret ~= 0 then
		return ret, res
	end
	if res.affected_rows <= 0 then
		return 17, "inner error"
	else
		return 0, "success"
	end 

end


function cmds:query_account(args)

	local pid =  args.pid
	local page = args.page
	local count =  args.count

	if not pid then
		if (not page) or (not count) then
			 return 11, "args error"
		end
	end

	if pid then
		if not tonumber(pid) then

			return 11, "pid error"
		end
	end

	if page then
		if not tonumber(page) then

			return 11, "page error"
		end
	end

	if count then
		if not tonumber(count) then

			return 11, "count error"
		end
	end


	local sql = ""
   
    if pid then
    	sql = string.format("select pid,name , fangka ,daili from player_base_moudle where pid =%s ", pid)
    else
    	sql = string.format("select pid,name , fangka ,daili from player_base_moudle limit %s,%s ", page*count, count)
    end
    return self:_query_(sql)
end


function cmds:get_player_count(args)
    local sql = string.format("select count(pid) as count from player_base_moudle ")
    return self:_query_(sql)
end


function cmds:set_gm(args)

	local pid = tonumber(args.pid)

	if not pid  then
		return  11, "args error"
	end

	local cmd_t = {}
	 cmd_t.cmd = "set_value"
	 cmd_t.pid = pid
	 cmd_t.props = {}
	table.insert(cmd_t.props,{0 ,"daili", 1}) --0设置,1增加

	local json_str = json.encode(cmd_t)

	local sql = string.format("insert into gm_cmd_moudle (info) VALUES ('%s')",json_str)

	local ret, res = self:_query_(sql)  

	if ret ~= 0 then
		return ret, res
	end
	if res.affected_rows <= 0 then
		return 17, "inner error"
	else
		return 0, "success"
	end 
end

function cmds:cancel_gm(args)

	local pid = tonumber(args.pid)

	if not pid  then
		return  11, "args error"
	end

	local cmd_t = {}
	 cmd_t.cmd = "set_value"
	 cmd_t.pid = pid
	 cmd_t.props = {}
	table.insert(cmd_t.props,{0 ,"daili", 0}) --0设置,1增加

	local json_str = json.encode(cmd_t)

	local sql = string.format("insert into gm_cmd_moudle (info) VALUES ('%s')",json_str)

	local ret, res = self:_query_(sql)  

	if ret ~= 0 then
		return ret, res
	end
	if res.affected_rows <= 0 then
		return 17, "inner error"
	else
		return 0, "success"
	end 
end


function cmds:sys_info(args)
	local info =args.info

	if not info  then
		return  11, "args error"
	end
	if string.find(info, " ") or
		string.find(info, "'") or
		string.find(info, "\"") then
		return  11, "sql inject"
	end

	local cmd_t = {}
	 cmd_t.cmd = "sys_info"
	 cmd_t.info = info

	local json_str = json.encode(cmd_t)

	local sql = string.format("insert into gm_cmd_moudle (info) VALUES ('%s')",json_str)

	local ret, res = self:_query_(sql)  

	if ret ~= 0 then
		return ret, res
	end
	if res.affected_rows <= 0 then
		return 17, "inner error"
	else
		return 0, "success"
	end 
end



return cmds